class_name UpTip
extends MarginContainer

#region 属性
@export var popup_text:RichTextLabel
@export var scale_y:float = 1
@export var scale_y_duration:float = 0.05
@export var scale_x:float = 1
@export var scale_x_duration:float = 0.25
var is_changed:bool = false
var _tween:Tween
var tip:String
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func show_tip(text:String):
	self.tip = text
func hide_tip():
	if _tween and _tween.is_valid():
		_tween.kill()
	_tween = _create_hide_tween()
#endregion
#region 私有方法
func _center(text:String) -> String:
	return "[center]{0}[/center]".format([text])
func _create_show_tween(text:String) -> Tween:
	var tween = create_tween()
	tween.tween_callback(func ():
		visible = true
		popup_text.clear()
		is_changed = true
		scale = Vector2(0.01,0)
		)
	tween.tween_property(self,"scale:y",scale_y,scale_y_duration).set_ease(Tween.EASE_IN)
	tween.tween_callback(func ():
		popup_text.append_text(_center(text))
		is_changed = true
		)
	tween.tween_property(self,"scale:x",scale_x,scale_x_duration).set_ease(Tween.EASE_OUT)
	return tween
func _create_hide_tween() -> Tween:
	var tween = create_tween()
	tween.tween_property(self,"scale:x",0.01,scale_x_duration).set_ease(Tween.EASE_OUT)
	tween.tween_callback(func ():
		popup_text.clear()
		is_changed = true
		)
	tween.tween_property(self,"scale:y",0,scale_y_duration).set_ease(Tween.EASE_OUT)
	tween.tween_callback(func ():
		visible = false
		queue_free()
		)
	return tween
func update_pivot_offset():
	pivot_offset = Vector2(size.x/2,size.y/2)
	is_changed = false
#endregion
#region 生命周期函数
func _init() -> void:
	update_pivot_offset()
func _ready() -> void:
	if _tween and _tween.is_valid():
		_tween.kill()
	_tween = _create_show_tween(tip)
func _process(delta: float) -> void:
	if is_changed and popup_text.is_ready():
		update_pivot_offset()
#endregion
